package com.usian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.EmpMapper;
import com.usian.mapper.PostsMapper;
import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private PostsMapper postsMapper;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(HttpSession session,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer pageSize, String name){
        Page<Emp> page1=empService.findPage(session,page,pageSize,name);
        return R.success(page1);
    }

    /**
     * 添加
     * @param emp
     * @return
     */
    @PostMapping
    public R add(@RequestBody Emp emp){
        Integer count1=empMapper.findByName(emp.getName());
        if (count1>0) {
            return R.error("员工名称重复！");
        }
        Integer code=postsMapper.findByCode(emp.getPid());
        if (code==0){
            Integer count=empMapper.findByPid(emp.getPid());
            if (count==1){
                return R.error("该部门董事长职位已满！");
            }
        }
        if (code==1){
            Integer count=empMapper.findByPid(emp.getPid());
            if (count==1){
                return R.error("该部门总经理职位已满！");
            }
        }
        if (code==2){
            Integer count=empMapper.findByPid(emp.getPid());
            if (count==2){
                return R.error("该部门副经理职位已满！");
            }
        }
        if (code==3){
            Integer count=empMapper.findByPid(emp.getPid());
            if (count==5){
                return R.error("该部门职员职位已满！");
            }
        }
        empService.add(emp);
        return R.success("添加成功");
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Integer id){
        return empService.findById(id);
    }

    /**
     * 修改
     * @param emp
     * @return
     */
    @PutMapping
    public R edit(@RequestBody Emp emp){
        return empService.edit(emp);
    }

    /**
     * 批删 - 单删
     * @param ids
     * @param session
     * @return
     */
    @DeleteMapping
    public R delete(String[] ids,HttpSession session){
        return empService.delete(ids,session);
    }
}



















